package com.hengyu.oauth2.entity;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.provider.ClientDetails;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.*;

/**
 * 项目名称：OAuth2Example
 * 项目版本：V1.0
 * 包名称：com.example.oauth.bean
 * 创建人：yuqy
 * 创建时间：2017/3/16 15:36
 * 修改人：yuqy
 * 修改时间：2017/3/16 15:36
 * 修改备注：
 */
@Entity
@Table(name = "oauth_client_details")
public class HengYuOAuth2ClientDetails implements Serializable,ClientDetails
{
    @Id
    @Column(name = "client_id")
    private String clientId;

    @Column(name = "resource_ids")
    private String resourceId;

    @Column(name = "client_secret")
    private String clientSecret;

    @Column(name = "scope")
    private String scope;

    @Column(name = "authorized_grant_types")
    private String authorizedGrantTypes;

    @Column(name = "web_server_redirect_uri")
    private String webServerRedirectUri;

    private String authorities;

    @Column(name = "access_token_validity")
    private int accessTokenValidity;

    @Column(name = "refresh_token_validity")
    private int refreshTokenValidity;

    @Column(name = "additional_information")
    private String additionalInformation;

    private String autoapprove;

    @Override
    public String getClientId() {
        return this.clientId;
    }

    @Override
    public Set<String> getResourceIds() {
        Set<String> resourceSet = new HashSet<String>();
        resourceSet.add(this.resourceId);
        return resourceSet;
    }

    @Override
    public boolean isSecretRequired() {
        return true;
    }

    @Override
    public String getClientSecret() {
        return this.clientSecret;
    }

    @Override
    public boolean isScoped() {
        return true;
    }

    @Override
    public Set<String> getScope() {
        Set<String> scopeSet = new HashSet<String>();
        scopeSet.add(this.scope);
        return scopeSet;
    }

    @Override
    public Set<String> getAuthorizedGrantTypes() {
        Set<String> grantSet = new HashSet<String>();
        grantSet.add(this.authorizedGrantTypes);
        return grantSet;
    }

    @Override
    public Set<String> getRegisteredRedirectUri() {
        Set<String> redirectSet = new HashSet<String>();
        redirectSet.add(this.webServerRedirectUri);
        return redirectSet;
    }

    @Override
    public Collection<GrantedAuthority> getAuthorities() {
        String authorities = this.authorities;
        GrantedAuthority grantedAuthority = new GrantedAuthority() {
            @Override
            public String getAuthority() {
                return authorities;
            }
        };
        Collection<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        grantedAuthorities.add(grantedAuthority);
        return grantedAuthorities;
    }

    @Override
    public Integer getAccessTokenValiditySeconds() {
        return this.accessTokenValidity;
    }

    @Override
    public Integer getRefreshTokenValiditySeconds() {
        return this.refreshTokenValidity;
    }

    @Override
    public boolean isAutoApprove(String s) {
        return true;
    }

    @Override
    public Map<String, Object> getAdditionalInformation() {
        return new HashMap<String,Object>(){{}};
    }
}
